/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 *
 * @author tho.hoang
 */
public class DateUtils {

    public static int getDayOfWeek() {

        Calendar cal = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("GMT+07:00");
        cal.setTimeZone(tz);

        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static int getDayOfWeek(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static String getCurrentDate() {
        int value = 0;
        String ts = "";
        String month[] = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        Calendar cal = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("GMT+07:00");
        cal.setTimeZone(tz);

        value = cal.get(Calendar.DAY_OF_MONTH);
        if (value < 10) {
            ts += "0" + value;
        } else {
            ts += value;
        }
        ts += "/" + month[cal.get(Calendar.MONTH)];
        ts += "/" + cal.get(Calendar.YEAR);

        return ts;
    }

    public static String dateToDDMMYYYY(Date date, String delimiter) {
        int value = 0;
        String result = "";
        String month[] = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        value = cal.get(Calendar.DAY_OF_MONTH);
        if (value < 10) {
            result += "0" + value;
        } else {
            result += value;
        }
        result += delimiter + month[cal.get(Calendar.MONTH)];
        result += delimiter + cal.get(Calendar.YEAR);

        return result;
    }

    public static String dateToYYYYMMDD(Date d) {

        String rs = "";
        String month[] = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        rs += cal.get(Calendar.YEAR);
        rs += month[cal.get(Calendar.MONTH)];
        int iday = cal.get(Calendar.DAY_OF_MONTH);

        if (iday < 10) {
            rs += "0" + iday;
        } else {
            rs += iday;
        }
        return rs;
    }

    public static Date getNow() {
        Calendar cal = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("GMT+07:00");
        cal.setTimeZone(tz);

        return cal.getTime();
    }

    public static Date addDays(Date date, int days) {
        long millisPerDay = 24 * 60 * 60 * 1000L;
        long t = date.getTime();
        t += millisPerDay * days;

        return new Date(t);
    }

    public static Date ddmmyyyytoDate(String ddmmyyyy) {
        Calendar cal = Calendar.getInstance();
        TimeZone tz = TimeZone.getTimeZone("GMT+07:00");
        cal.setTimeZone(tz);

        int day = Integer.parseInt(ddmmyyyy.substring(0, 2));
        int mon = Integer.parseInt(ddmmyyyy.substring(2, 4));
        int yea = Integer.parseInt(ddmmyyyy.substring(4));

        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.MONTH, mon - 1);
        cal.set(Calendar.YEAR, yea);

        return cal.getTime();
    }

    public static int compare2Date(Date one, Date two) {
        if (one == null && two == null) {
            return 0;
        }
        if (one == null) {
            return -1;
        }
        if (two == null) {
            return 1;
        }

        int dayOne, dayTwo, monOne, monTwo, yearOne, yearTwo;

        Calendar calOne = Calendar.getInstance();
        calOne.setTime(one);
        yearOne = calOne.get(Calendar.YEAR);
        monOne = calOne.get(Calendar.MONTH);
        dayOne = calOne.get(Calendar.DAY_OF_MONTH);

        Calendar calTwo = Calendar.getInstance();
        calTwo.setTime(two);
        yearTwo = calTwo.get(Calendar.YEAR);
        monTwo = calTwo.get(Calendar.MONTH);
        dayTwo = calTwo.get(Calendar.DAY_OF_MONTH);

        if (yearOne < yearTwo) {
            return -1;
        }
        if (yearOne > yearTwo) {
            return 1;
        }
        if (monOne < monTwo) {
            return -1;
        }
        if (monOne > monTwo) {
            return 1;
        }
        if (dayOne < dayTwo) {
            return -1;
        }
        if (dayOne > dayTwo) {
            return 1;
        }
        return 0;
    }
}
